package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.modules.app.entity.*;
import com.zjmzxfzhl.modules.app.form.AppDocVo;
import com.zjmzxfzhl.modules.app.form.AppProdVerVo;
import com.zjmzxfzhl.modules.app.form.AppProdVo;
import com.zjmzxfzhl.modules.app.form.DocFilePath;
import com.zjmzxfzhl.modules.app.service.*;
import com.zjmzxfzhl.modules.flowable.service.ProcessInstanceService;
import com.zjmzxfzhl.modules.flowable.vo.ProcessInstanceRequest;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.mapper.AppProdVerMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppProdVerServiceImpl extends BaseServiceImpl<AppProdVerMapper, AppProdVer>
        implements AppProdVerService {

    @Autowired
    private AppLogService appLogService;
    @Autowired
    private ProcessInstanceService processInstanceService;
    @Autowired
    private AppDocService appDocService;

    @Override
    public IPage<AppProdVerVo> list(IPage<AppProdVerVo> page, AppProdVerVo appProdVerVo) {
        return page.setRecords(baseMapper.list(page, appProdVerVo));
    }
    @Override
    public List<AppProdVerVo> getList(IPage<AppProdVerVo> page, AppProdVerVo appProdVerVo) {
        return baseMapper.list(page, appProdVerVo);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppProdVerVo queryById(String id) {
        AppProdVerVo appProdVerVo=new AppProdVerVo();
        appProdVerVo.setVerId(id);
        List<AppProdVerVo> appProdVerVoList= baseMapper.list(null, appProdVerVo);
        if(appProdVerVoList.size()>0){
            return appProdVerVoList.get(0);
        }
        return null;
    }
    /**
     * 待审批
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pendingTrial(String verId) {
        AppProdVer appProdVer=this.getById(verId);
        if(appProdVer==null){
            return false;
        }
        appProdVer.setStatus("00");
        appProdVer.setUpdateTime(new Date());
        Boolean result = this.updateById(appProdVer);

        appLogService.addLog("变更产品发布状态为 待审批","07",appProdVer.getVerId());
        return result;
    }

    /**
     * 审批中
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean inReview(String verId) {
        AppProdVer appProdVer=this.getById(verId);
        if(appProdVer==null){
            return false;
        }
        appProdVer.setStatus("01");
        appProdVer.setUpdateTime(new Date());
        Boolean result = this.updateById(appProdVer);

        appLogService.addLog("变更产品发布状态为 审批中","07",appProdVer.getVerId());
        return result;
    }

    /**
     * 审批过
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean reviewed(String verId) {
        AppProdVer appProdVer=this.getById(verId);
        if(appProdVer==null){
            return false;
        }
        appProdVer.setStatus("02");
        appProdVer.setUpdateTime(new Date());
        Boolean result = this.updateById(appProdVer);

        appLogService.addLog("变更产品发布状态为 审批过","07",appProdVer.getVerId());
        return result;
    }

    /**
     * 已驳回
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean reject(String verId) {
        AppProdVer appProdVer=this.getById(verId);
        if(appProdVer==null){
            return false;
        }
        appProdVer.setStatus("03");
        appProdVer.setUpdateTime(new Date());
        Boolean result = this.updateById(appProdVer);

        appLogService.addLog("变更产品发布状态为 已驳回","07",appProdVer.getVerId());
        return result;
    }

    /**
     * 已关闭
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean close(String verId) {
        AppProdVer appProdVer=this.getById(verId);
        if(appProdVer==null){
            return false;
        }
        appProdVer.setStatus("04");
        appProdVer.setUpdateTime(new Date());
        Boolean result = this.updateById(appProdVer);

        appLogService.addLog("变更产品发布状态为 已关闭","07",appProdVer.getVerId());
        return result;
    }
    /**
     * 启动文档审批流程实例
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processStart(String verId) {

        AppProdVer appProdVer=this.getById(verId);
        if(appProdVer==null){
            return false;
        }
        if(appProdVer.getModelId()==null){
            throw new FlowableException("发起流程失败！检查是否存在流程！");
        }
        if("01".equals(appProdVer.getStatus())||"02".equals(appProdVer.getStatus())){
            throw new FlowableException("已发起过流程！");
        }
        ProcessInstanceRequest processInstanceRequest=new ProcessInstanceRequest();
        processInstanceRequest.setProcessDefinitionId(appProdVer.getModelId());
        processInstanceRequest.setBusinessKey("prodVer:"+appProdVer.getVerId());
        ProcessInstance instance = processInstanceService.start(processInstanceRequest);
        appProdVer.setProcessInstanceId(instance.getProcessInstanceId());
        appProdVer.setUpdateTime(new Date());
        this.updateById(appProdVer);

        Boolean result=inReview(appProdVer.getVerId());
        return result;
    }

    /**
     * 查询审核文档
     *
     * @param processInstanceId
     * @return
     */
    public List<AppProdVerVo> procProdVer(String processInstanceId) {
        return baseMapper.procProdVer(processInstanceId);
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProdVer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppProdVer(AppProdVer appProdVer){
        List<DocFilePath> docFilePaths= null;
        if(appProdVer.getDirId()==null||"".equals(appProdVer.getDirId())){
            appProdVer.setDirId("");
        }
        if(appProdVer.getDocFilePath()!=null){
            docFilePaths=appProdVer.getDocFilePath();
        }
        //添加项目
        Boolean result = this.save(appProdVer);
        if(result==false){
            return false;
        }
        appProdVer=this.getById(appProdVer.getVerId());
        //添加变更附件
        if (docFilePaths != null && docFilePaths.size() > 0) {
            AppDoc appDoc=new AppDoc();
            appDoc.setDocFilePath(docFilePaths);
            appDoc.setDeleted("0");
            appDoc.setName("产品发布附件");
            appDoc.setDirId("##");
            appDoc.setDocType("09");
            appDoc.setDocTypeId(appProdVer.getVerId());
            appDoc.setContent("产品发布附带文件");
            appDocService.saveAppDoc(appDoc);
        }
        return result;
    }

    @Override
    public Integer selectRowCount(AppProdVerVo appProdVerVo) {
        return baseMapper.selectRowCount(appProdVerVo);
    }
}
